//
// Created by francklinson on 2021/8/17 AT 9:00.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>

using namespace std;

class Solution {
public:
    bool checkRecord(string s) {
        int absence = 0; // 缺勤数量
        int firstLate = 0, lastLate = 0; // 连续迟到的第一天和最后一天
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == 'A') {
                ++absence;
                if (absence >= 2)
                    return false;
            } else if (s[i] == 'L') {
                if (i > 0 and s[i - 1] == 'L') {
                    ++lastLate;
                    if (lastLate - firstLate >= 2) {
                        return false;
                    }
                } else {
                    firstLate = lastLate = i;
                }
            }
        }
        return true;
    }
};

class Solution2 { // 题解
public:
    bool checkRecord(string s) {
        int absents = 0, lates = 0;  // 一个变量记录连续迟到就行
        for (auto &ch : s) {
            if (ch == 'A') {
                absents++;
                if (absents >= 2) {
                    return false;
                }
            }
            if (ch == 'L') {
                lates++;
                if (lates >= 3) {
                    return false;
                }
            } else {
                lates = 0;
            }
        }
        return true;
    }
};


int main() {
    Solution solution;
    cout << solution.checkRecord("PPALALP") << endl;
    return 0;
}

